package slidingWindow;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

/*
 *@description 无重复字符的最长子串
 *@author hkfan
 *@version 1.0
 *@create 2025/5/28 14:06
 */
public class LeetCode_3 {

    public int lengthOfLongestSubstring(String s) {
        //1. 定义变量
        int i=0,j=0,res=0;
        Set<Character> set = new HashSet<>();
        //2. 遍历
        while (j<s.length()){
            if (!set.contains(s.charAt(j))){
                set.add(s.charAt(j));
                j++;
                res = Math.max(res,set.size());
            }else {
                while (s.charAt(i)!=s.charAt(j)){
                    set.remove(s.charAt(i));
                    i++;
                }
                set.remove(s.charAt(i));
                i++;
            }
        }
        return res;
    }



}
